package org.androidpush.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.androidpush.R;
import org.androidpush.client.Constants;
import org.androidpush.model.CouScoreDto;
import org.androidpush.utils.ConstantUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.HttpUrl;

/**
 * Created by huang on 2017/4/16 0016.
 */
public class CouScoreActivity extends Activity{

    private ListView couScoreLV;
    private String userId,requestWay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xml_listview);
        init();
        ConstantUtils.aetActionBar(this, getString(R.string.str_scorse));
        String url = ConstantUtils.URL+"JxhtWeb/StuCouController/"+userId+"/getStuScore?requestWay="+ requestWay;
        Log.d("URL",url);
        new couDetailAsyncTask().execute(url);
    }

    private void init(){
        SharedPreferences sp = CouScoreActivity.this.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_APPEND);
        userId = sp.getString(ConstantUtils.USER_ID, "");
        Log.d("Status",sp.getString(ConstantUtils.STATUS, "NULL"));
        requestWay = ConstantUtils.getStatusStrWithId(sp.getString(ConstantUtils.STATUS,ConstantUtils.STUDENT));
        couScoreLV = (ListView) findViewById(R.id.id_listview_list);
    }

    private List<CouScoreDto> getCouDetailDate(final String url){
        List<CouScoreDto> courseDtos = new ArrayList<>();
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()){
                String result = (String) response.body().string();
//                Log.d("onResponse", result);
                courseDtos = ConstantUtils.parseJsonArrayWithGson(result, CouScoreDto.class);
//                Log.d("SelectCourseDto",courseDtos.toString());
            }else {
                throw new IOException("Unexpected code " + response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return courseDtos;
    }

    class couDetailAsyncTask extends
            AsyncTask<String, Void, List<CouScoreDto>> {

        @Override
        protected List<CouScoreDto> doInBackground(String... params) {
            return getCouDetailDate(params[0]);
        }

        @Override
        protected void onPostExecute(List<CouScoreDto> result) {
            super.onPostExecute(result);
            List<Map<String,Object>> datalist = getData(result);
            SimpleAdapter adapter  = new SimpleAdapter(CouScoreActivity.this,datalist,R.layout.xml_selectcou_detail_item,
                    new String[] {"name","score"},new int[]{R.id.id_coudetailitem_name,R.id.id_coudetailitem_value});
            couScoreLV.setAdapter(adapter);
            couScoreLV.setCacheColorHint(Color.TRANSPARENT);
        }
    }

    private List<Map<String,Object>> getData(List<CouScoreDto> couScoreDtos){
        List<Map<String,Object>> datalist = new ArrayList<Map<String, Object>>();

        for(CouScoreDto dto:couScoreDtos){
            HashMap<String, Object>  itemData = new HashMap<>();
            itemData.put("name",dto.getCouName());
            itemData.put("score", dto.getstuCouScore());
            datalist.add(itemData);
        }
        return datalist;
    }
}
